<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 9:33
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;

class DbManage extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $tables   = Db::query("show table status from " . config('database')['database']);
            $list     = [];
            $path     = RUNTIME_PATH.'data';
            $handler  = opendir($path);
            while( ($filename = readdir($handler)) !== false )
            {
                if($filename != '.' && $filename != '..')
                {
                    array_push($list, ['filename'=>$filename, 'size'=>filesize($path.DS.$filename), 'create_time'=>filemtime($path.DS.$filename)]);
                }
            }
            closedir($handler);
            return view('index', [
                'tables'=>$tables,
                'list'=>$list
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function backup()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'backup', $this->_juri);
            if ($chk['status'])
            {
                $dbManage = new \other\DbManage();
                $res      = $dbManage->backup();
                return $res;
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }
        }
    }

    public function del()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $filename = input('param.filename');
                $path     = RUNTIME_PATH.'data'.DS.$filename;
                if (file_exists($path))
                {
                    @unlink($path);
                    return ['status'=> true, 'msg'=>'删除成功！'];
                } else {
                    return ['status'=> false, 'msg'=>'备份文件不存在！'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }
        }
    }

    public function importDatabase()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'importDatabase', $this->_juri);
            if ($chk['status'])
            {
                $filename = input('param.filename');
                $path     = RUNTIME_PATH.'data'.DS.$filename;
                if (file_exists($path))
                {
                    $dbManage = new \other\DbManage();
                    $res      = $dbManage->_import($path);
                    return $res;
                } else {
                    return ['status'=> false, 'msg'=>'备份文件不存在！'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}